/* 1636. 按照频率将数组升序排序 */
/**
 * @param {number[]} nums
 * @return {number[]}
 */
var frequencySort = function (nums) {
	const map = new Map()
	const result = []
	let maxSize = 0
	nums.forEach((num) => {
		map.set(num, (map.get(num) || 0) + 1)
		maxSize = Math.max(maxSize, map.get(num))
	})
	for (let i = 1; i <= maxSize; i++) {
		let tem = []
		for (const [key, value] of map) {
			if (value === i) {
				tem.push(key)
			}
		}
		if (!tem.length) continue
		tem.sort((a, b) => b - a)
		tem.forEach((item) => {
			for (let j = 0; j < i; j++) {
				result.push(item)
			}
		})
	}
	return result
}
var frequencySort = function (nums) {
	const map = new Map()
	nums.forEach((num) => {
		map.set(num, (map.get(num) || 0) + 1)
	})
	// const map = nums.reduce(
	// 	(prev, next) => prev.set(next, (prev.get(next) || 0) + 1),
	// 	new Map()
	// )
	const list = [...nums]
	list.sort((a, b) => {
		const cnt1 = map.get(a),
			cnt2 = map.get(b)
		return cnt1 !== cnt2 ? cnt1 - cnt2 : b - a
	})
	nums.forEach((_, i) => {
		nums[i] = list[i]
	})
	return nums
}
frequencySort([-1, 1, -6, 4, 5, -6, 1, 4, 1])
